$(document).ready(function() {
    editPost.init();
});

var editPost = (function() {
    
    function saveEditPost() {
        $('.errorHandler').html('');

        if (!$('#profileId').val()) {
            $('#profileId').parents('.input-group').addClass('has-error');
            $('#editProfile').find('i.fa').attr('class', 'fa fa-save');
            return false;
        }
        if (!$('#profileName').val()) {
            $('#profileName').parents('.input-group').addClass('has-error');
            $('#editProfile').find('i.fa').attr('class', 'fa fa-save');
            return false;
        }

        $('#profileContent').val(tinyMCE.activeEditor.getContent());
        $('#profileInput').ajaxSubmit({
            url: '/profile/ajax-edit-profile',
            type: 'post',
            dataType: 'json',
            timeout: 3000,
            success: function(resp) {
                $('.errorHandler').html('<small><small><strong class="text-success">' + resp.msg + '</strong></small></small>').show();
                $('#editProfile').find('i').attr('class', 'fa fa-save');
                $('#profileAvatar').val('');
            }
        });
    }
    
    function deletePost(profileId, profileName,callback) {
        showConfirm({
            msg: 'Bạn có chắc chắn muốn xoá thông tin người này không?',
            title: 'Xoá Post',
            noBt: 'Đóng',
            yesBt: 'Đồng ý xoá',
            onYes: function() {
                $.post('/profile/ajax-delete-profile', {id: profileId, name: profileName}, function(resp) {
                    if (resp.err == 0) {
                        callback();
                        hideConfirm();
                    } else {
                        $('.errorHandler').html('<small><small><strong class="text-danger">' + resp.msg + '</strong></small></small>').show();
                    }
                }, 'json');
            }
        });
    }
    function initHanlder() {
        initWysiwyg();

        $('#editProfile').click(function() {
            $(this).find('i.fa').attr('class', 'fa fa-spinner fa-spin');
            saveEditPost();
            return false;
        });
        
        $('#deleteProfile').click(function() {
            var postId = $('#profileId').val();
            var postName = $('#profileName').val();
            deletePost(postId, postName,function() {
                window.location.href = '/profile';
            });
            return false;
        });
        var hash = window.location.hash;
        if (hash.indexOf('#msg=') > -1) {
            $('.errorHandler').html('<small><small><strong class="text-danger">' + hash.substring(5) + '</strong></small></small>').show();
            window.location.hash = '';
        }
    }
    return {
        init: function() {
            $('.media > a').tooltip();
            initHanlder();
            initDatePicker('#profileDOB');
        },
        deletePost: function(tag, postId) {
            deletePost(postId, function() {
                $(tag).parents('tr')[0].remove();
            });
        }
    };
})();